/*
 * Created on 20/02/2013 - 6:41 PM
 * 
 */
package dxfpresentation;

import java.awt.Frame;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author adriano
 */
public class OpenNewProject extends javax.swing.JDialog {

    /**
     * Creates new form OpenNewProject
     */
    public OpenNewProject(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public String getProjectPath()
    {
        return projectPath;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        pnlNewProject = new javax.swing.JPanel();
        lblProjectName = new javax.swing.JLabel();
        lblDirectoryName = new javax.swing.JLabel();
        txtProjectName = new javax.swing.JTextField();
        txtDirectoryName = new javax.swing.JTextField();
        btnSearchPath = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnCancelNewProject = new javax.swing.JButton();
        btnConfirmNewProject = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pnlNewProject.setBorder(javax.swing.BorderFactory.createTitledBorder("Novo Projeto"));

        lblProjectName.setText("Nome do Projeto:");

        lblDirectoryName.setText("Diretório:");

        btnSearchPath.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dxfpresentation/resources/folder_explore.png"))); // NOI18N
        btnSearchPath.setText("Procurar...");
        btnSearchPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchPathActionPerformed(evt);
            }
        });

        btnCancelNewProject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dxfpresentation/resources/Cancelar.png"))); // NOI18N
        btnCancelNewProject.setText("Cancelar");
        btnCancelNewProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelNewProjectActionPerformed(evt);
            }
        });

        btnConfirmNewProject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dxfpresentation/resources/confirmar.png"))); // NOI18N
        btnConfirmNewProject.setText("Confirmar");
        btnConfirmNewProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmNewProjectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlNewProjectLayout = new javax.swing.GroupLayout(pnlNewProject);
        pnlNewProject.setLayout(pnlNewProjectLayout);
        pnlNewProjectLayout.setHorizontalGroup(
            pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewProjectLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(pnlNewProjectLayout.createSequentialGroup()
                        .addComponent(lblProjectName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtProjectName))
                    .addGroup(pnlNewProjectLayout.createSequentialGroup()
                        .addComponent(lblDirectoryName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtDirectoryName, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewProjectLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSearchPath, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewProjectLayout.createSequentialGroup()
                                .addComponent(btnConfirmNewProject)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelNewProject, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        pnlNewProjectLayout.setVerticalGroup(
            pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewProjectLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProjectName)
                    .addComponent(txtProjectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDirectoryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDirectoryName))
                .addGap(14, 14, 14)
                .addComponent(btnSearchPath)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlNewProjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnConfirmNewProject)
                    .addComponent(btnCancelNewProject))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlNewProject, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlNewProject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelNewProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelNewProjectActionPerformed

        // this event does nothing, just close new project panel
        // close new project panel window
        this.dispose();
    }//GEN-LAST:event_btnCancelNewProjectActionPerformed

    private void btnConfirmNewProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmNewProjectActionPerformed

        if ("".equals(txtProjectName.getText()) || "".equals(txtDirectoryName.getText())) {
            showWarningMessage("Diretório ou nome de Projeto não especificado!");
        } else {
            File directory;
            File processingSubDirectory;
            File postProcessingSubDirectory;

            try {
                directory = new File(txtDirectoryName.getText() + File.separator + txtProjectName.getText());

                if (!directory.exists()) 
                {
                    directory.mkdir();
                    // create processing and postprocessing sub directories for current project
                    processingSubDirectory = new File(directory.getAbsolutePath() + File.separator + "pre_processing");
                    processingSubDirectory.mkdir();
                    postProcessingSubDirectory = new File(directory.getAbsolutePath() + File.separator + "post_processing");
                    postProcessingSubDirectory.mkdir();
                    projectPath = directory.getAbsolutePath();
                    
                    this.dispose();
                } 
                else 
                {
                    showWarningMessage("Diretório já existente!");
                }
            } 
            
            catch (Exception ex) 
            {}
        }
    }//GEN-LAST:event_btnConfirmNewProjectActionPerformed

    private void btnSearchPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchPathActionPerformed

        // looking for a project in default path
        fileChooser = new JFileChooser(System.getProperty("user.dir") + File.separator + "simulation");
        // only directories
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle("Escolher Diretório");

        int returnVal = fileChooser.showOpenDialog(null);
        // Teste de Seleção do Usuário
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String fullFileName = fileChooser.getSelectedFile().toString();
            txtDirectoryName.setText(fullFileName);
        } else if (returnVal == JFileChooser.CANCEL_OPTION) {
        }
    }//GEN-LAST:event_btnSearchPathActionPerformed

    private void showWarningMessage(String message) {
    
        JOptionPane.showMessageDialog(new Frame(), 
                                          message, 
                                          "Mensagem de Alerta", 
                                          JOptionPane.WARNING_MESSAGE);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OpenNewProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OpenNewProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OpenNewProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OpenNewProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                OpenNewProject dialog = new OpenNewProject(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private String projectPath;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelNewProject;
    private javax.swing.JButton btnConfirmNewProject;
    private javax.swing.JButton btnSearchPath;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblDirectoryName;
    private javax.swing.JLabel lblProjectName;
    private javax.swing.JPanel pnlNewProject;
    private javax.swing.JTextField txtDirectoryName;
    private javax.swing.JTextField txtProjectName;
    // End of variables declaration//GEN-END:variables
}
